package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentServicr;

import java.util.Scanner;

public abstract class BaseStudentController {

    private StudentServicr servicr =new StudentServicr();
    private Scanner s = new Scanner(System.in);


    public void start() {

        cc:
        while (true) {
            System.out.println("-----------------------欢迎使用<学生>管理系统---------------------------");
            System.out.println("请输入你的选择: 1.添加学生  2. 删除学生  3.修改学生 4. 查看学生 5.退出");

            String choce = s.next();
            switch (choce) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    lookStudent();
                    break;
                case "5" :
                    System.out.println("感谢使用");
                    break cc;
                default:
                    System.out.println("你输入有误,请重新输入");
                    break ;
            }
        }

    }

    private void updateStudent() {
        String updateId = judgeId();

        Student newStu = newStu(updateId);
        servicr.updateStudent(newStu,updateId);

        System.out.println("修改成功");

    }

    private String judgeId() {
        String updateId;
        while (true) {
            System.out.println("请输入学号");
            updateId = s.next();
            boolean flag = servicr.isExists(updateId);
            if (!flag) {
                System.out.println("你输入的学号不存在,请你重新输入");
            } else {

                break;
            }
        }
        return updateId;
    }

    private void deleteStudent() {
        String delId = judgeId();
        servicr.deleteStudent(delId);
        System.out.println("删除成功");

    }


    private void lookStudent() {

        Student [] stu = servicr.findAllStudent();

        if (stu == null){
            System.out.println("未存在学生信息,请添加后查看");
            return;
        }
        System.out.println("学号+\t"+"\t姓名"+"\t年龄\t\t"+"生日");
        //遍历数组
        for (int i = 0; i < stu.length; i++) {
            Student stus = stu[i];
            if (stus != null){
                System.out.println(stus.getId()+"\t"+stus.getName()+"\t"+stus.getAge()+"\t\t"+stus.getBirthday());
            }

        }

    }

    private void addStudent() {


        //键盘录入

        String id;
        System.out.println("请输入学号");
        while(true){

            id = s.next();
            boolean exists = servicr.isExists(id);
            if (exists){
                System.out.println("学号已存在,请你重新输入");
            }else {
                break;
            }
        }
        Student stu = newStu(id);



        boolean flag = servicr.addStudent(stu);

        if (flag){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }

    public abstract Student newStu(String id) ;

}
